package com.atguigu.pratice;

public class FindKthTail {
    public static ListNode findTheKthToTail(ListNode head,int k){
        if(head==null){
            return null;
        }
        int count=0;
        ListNode cur=head;
        while(cur!=null){
            count++;
            cur=cur.next;
        }
        if(k>count){
            return null;
        }
        //接下来都是合法的
        ListNode slow=head;
        ListNode fast=head;
        while(k>0&&fast!=null){
            fast=fast.next;
            k--;
        }
        while(fast!=null){
            slow=slow.next;
            fast=fast.next;
        }
        return slow;
    }
}
